package com.cskaoyan.service;

import com.cskaoyan.bean.bo.CouponCreateBo;
import com.cskaoyan.bean.bo.CouponDeleteBo;
import com.cskaoyan.bean.bo.CouponUpdateBo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.CouponPo;
import com.cskaoyan.bean.po.CouponReadPo;
import com.cskaoyan.bean.vo.CouponListUserVo;
import com.cskaoyan.bean.vo.CouponListVo;
import com.cskaoyan.bean.vo.CouponReadVo;
import com.cskaoyan.bean.vo.CouponVo;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.util.StringToArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: 陈慧颖
 * @time: Created by chy on 2022/6/4 15:15
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    CouponMapper couponMapper;

    /**
     * 查询所有优惠券信息
     *
     * @param baseParam
     * @return
     */
    @Override
    public CouponListVo listAllCoupon(BaseParam baseParam ,String name  ,Integer type, Integer status) {
        // sql 查询所有数据
        List<CouponVo> couponVos = couponMapper.listAllCoupon(baseParam,name,type,status);
        // sql 获取总数量
        int total = couponMapper.queryAllCount();
        // 计算获取 pages 和 total;
        int pages = 0;
        try {
            pages = total / baseParam.getLimit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // new couponListVo
        CouponListVo couponListVo = new CouponListVo();
        // 将查询得到的couponVos给到couponListVo;
        couponListVo.setList(couponVos);
        couponListVo.setLimit(baseParam.getLimit());
        couponListVo.setPage(baseParam.getPage());

        couponListVo.setPages(pages);
        couponListVo.setTotal(total);
        return couponListVo;
    }

    /**
     * 接收 VXCollectAddOrDeleteBo 对象, 转为 Po 与 sql 交互
     *
     * @param couponCreateBo
     * @return
     */
    @Override
    public CouponVo create(CouponCreateBo couponCreateBo) {
        // 将 VXCollectAddOrDeleteBo 转化为 Po
        CouponPo couponPo = new CouponPo();
        couponPo.setDays(couponCreateBo.getDays());
        couponPo.setDesc(couponCreateBo.getDesc());
        couponPo.setDiscount(couponCreateBo.getDiscount());
        couponPo.setEndTime(couponCreateBo.getEndTime()); //couponCreateBo.getEndTime()
        couponPo.setGoodsType(couponCreateBo.getGoodsType());
        // 特别注意;couponPo对象的 成员变量GoodsValue的 类型是List<?> ; 客户端传递的就是 list
        /**
         * 经过试错后, 必须将couponPo的GoodsValue成员必须是 String 类型
         */
        List<?> goodsValue = couponCreateBo.getGoodsValue(); // list->String;
        couponPo.setGoodsValue(goodsValue.toString());

        couponPo.setLimit(couponCreateBo.getLimit());
        couponPo.setMin(couponCreateBo.getMin());
        couponPo.setName(couponCreateBo.getName());
        couponPo.setStartTime(couponCreateBo.getStartTime());
        couponPo.setStatus(couponCreateBo.getStatus());
        couponPo.setTag(couponCreateBo.getTag());
        couponPo.setTimeType(couponCreateBo.getTimeType());
        couponPo.setTotal(couponCreateBo.getTotal());
        couponPo.setType(couponCreateBo.getType());//
        // endTime 和 startTime 由客户端提供; 也可能不写 那就是 null;
        // 由 po 对象提供 addTime 和 updateTime
        couponPo.setAddTime(new Date());
        couponPo.setUpdateTime(new Date());
        couponPo.setDeleted(0); // create 之后就默认设定 0 ;

        // 与 sql 交互;
        int count  = couponMapper.create(couponPo);
        // 返回给 controller;
        CouponVo couponVo  =new CouponVo();  // 不设置 id;
        // 可能客户不写时间类信息;
        couponVo.setAddTime(couponPo.getAddTime());
        couponVo.setDays(couponPo.getDays());
        couponVo.setDesc(couponPo.getDesc());
        couponVo.setDiscount(couponPo.getDiscount());
        couponVo.setGoodsType(couponPo.getGoodsType());
        // 为了兼容  "[]"    []
        // String goodsValue1 = couponPo.getGoodsValue();
        // couponVo.setGoodsValue(StringToArrayUtils.toArray(goodsValue1));
        couponVo.setGoodsValue(couponCreateBo.getGoodsValue());
        couponVo.setLimit(couponPo.getLimit());

        couponVo.setType(couponCreateBo.getType());// VO 从 Bo 获取;  而不是 Vo 从 Po 获取;
        /**
         * 设置数据库表ID为自增长；
         * 配置@Options(useGeneratedKeys=true, keyProperty=”对象.属性”) 这个的作用是设置是否使用JDBC的getGenereatedKeys()
         * 方法获取主键并赋值到keyProperty设置的对象的属性中，说白了就是把自增长的主键值赋值给对象相应的属性；
         * 在插入后，使用对象.主键属性的getXXId()方法 获取主键值；
         */
        couponVo.setId(couponPo.getId());
        couponVo.setMin(couponPo.getMin());
        couponVo.setName(couponPo.getName());
        couponVo.setStatus(couponPo.getStatus());
        couponVo.setTag(couponPo.getTag());
        couponVo.setTimeType(couponPo.getTimeType());
        couponVo.setTotal(couponPo.getTotal());
        couponVo.setUpdateTime(couponPo.getUpdateTime());
        // couponVo.setDeleted(false); // 新建的 coupon 默认是 false;

        return couponVo;
    }

    /**
     *
     * 根据响应参数, 复用couponUpdateBo; 返回的couponUpdateBo的 daysType 为 null;
     * todo: 一旦使用goodsValue;总是报错;
     * @param couponUpdateBo
     * @return
     */
    @Override
    public CouponUpdateBo updateOneCoupon(CouponUpdateBo couponUpdateBo) {
        // 仅仅需要修改入参: couponUpdateBo的updateTime,其余原样返回即可;
        //new Date() 的时间即为: 更新成功的时间
        // BO->PO;
        CouponPo couponPo = new CouponPo();
        couponPo.setAddTime(couponUpdateBo.getAddTime());
        couponPo.setDays(couponUpdateBo.getDays());
        couponPo.setTimeType(couponUpdateBo.getDaysType());// 可能有异常;
        // couponPo.setDeleted(couponUpdateBo.getDeleted()==true?1:0); // couponUpdateBo.getDeleted()  是 null
        System.out.println(couponUpdateBo);
        System.out.println(couponUpdateBo.getDeleted());
        // couponPo.setDesc(couponUpdateBo.getDesc());
        couponPo.setDiscount(couponUpdateBo.getDiscount());
        couponPo.setGoodsType(couponUpdateBo.getGoodsType());
        List<?> goodsValue = couponUpdateBo.getGoodsValue();  //  "[]"
        if (goodsValue==null) {
            goodsValue = new ArrayList();
        }
        System.out.println("goodsValue = " + goodsValue);
        couponPo.setGoodsValue(goodsValue.toString());
        couponPo.setId(couponUpdateBo.getId());
        couponPo.setLimit(couponUpdateBo.getLimit());
        couponPo.setMin(couponUpdateBo.getMin());
        couponPo.setName(couponUpdateBo.getName());
        couponPo.setStatus(couponUpdateBo.getStatus());
        // couponPo.setTag(couponUpdateBo.getTag());
        couponPo.setTimeType(couponUpdateBo.getTimeType());
        couponPo.setTotal(couponUpdateBo.getTotal());
        couponPo.setType(couponUpdateBo.getType());
        couponPo.setUpdateTime(new Date());
        couponPo.setGoodsValue(null);
        int count =   couponMapper.updateOneCoupon(couponPo);
        return couponUpdateBo;
    }
    
    @Override
    public void deleteOneById(CouponDeleteBo couponDeleteBo) {
         int count = couponMapper.deleteOne(couponDeleteBo);
    }

    /**
     * 核心逻辑; 获取到controller 传递的 baseParam 对象和 couponId;
     * @param baseParam
     * @param couponId
     * @return
     */
    @Override
    public CouponListUserVo listuserByCouponId(BaseParam baseParam, Integer couponId) {
        System.out.println("in service:  baseParam = " + baseParam);
        System.out.println("in service : couponId = " + couponId);

        CouponListUserVo couponListUserVo =        couponMapper.listuserByCouponId(baseParam,couponId);
        return couponListUserVo;
    }

    @Override
    public CouponReadVo read(Integer id) {
        // 一般地, sql 查询出来的可以使用`as`来 直接返回 Vo 对象, 但是 类型不同,所以sql 必须返回 Po; 在 service 层将 Po->VXCollectAddOrDeleteBo;
        CouponReadPo couponReadPo   =  couponMapper.readCouponById(id);
        // Po->Vo
        CouponReadVo couponReadVo = new CouponReadVo();
        couponReadVo.setId(couponReadPo.getId());
        couponReadVo.setName(couponReadPo.getName());
        couponReadVo.setDesc(couponReadPo.getDesc());
        couponReadVo.setTag(couponReadPo.getTag());
        couponReadVo.setTotal(couponReadPo.getTotal());
        couponReadVo.setDiscount(couponReadPo.getDiscount());
        couponReadVo.setMin(couponReadPo.getMin());
        couponReadVo.setLimit(couponReadPo.getLimit());
        couponReadVo.setType(couponReadPo.getType());
        couponReadVo.setStatus(couponReadPo.getStatus());
        couponReadVo.setGoodsType(couponReadPo.getGoodsType());
        /**
         * 将 varchar -> list: 注意避免 NPE 空指针异常;
         */
        String goodsValue = couponReadPo.getGoodsValue();
        List<Integer> integers = StringToArrayUtils.toArray(goodsValue);
        couponReadVo.setGoodsValue(integers);
        couponReadVo.setCode(couponReadPo.getCode());
        couponReadVo.setTimeType(couponReadPo.getTimeType());
        couponReadVo.setDays(couponReadPo.getTimeType());
        couponReadVo.setAddTime(couponReadPo.getAddTime());
        couponReadVo.setUpdateTime(couponReadPo.getUpdateTime());
        couponReadVo.setDeleted(couponReadPo.getDeleted());

        return couponReadVo;
    }
}
